<template>
    <div class="mk">
        <el-upload
      class="avatar-uploader"
      action="http://182.92.156.134:8080/upload"
      name="file"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      >
      <img width="100" height="100" v-if="ruleForm.cover_img" :src="ruleForm.cover_img" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="课程名称" prop="name" style="width:50%;">
                <el-input v-model="ruleForm.name" ></el-input>
            </el-form-item>
            <el-form-item label="学科分类" prop="type" >
                <el-select v-model="ruleForm.type" placeholder="请选择课程">
                <el-option v-for="item in classType.classtype " :key="item.id" :label="item.type" :value="item.type"></el-option>
                </el-select>
            </el-form-item>
             <el-form-item label="课程价格" prop="price" style="width:50%;">
                <el-input v-model="ruleForm.price"></el-input>
            </el-form-item>
            <el-form-item label="课程老师" prop="teacher" style="width:50%;">
                <el-input v-model="ruleForm.teacher"></el-input>
            </el-form-item>
            <el-form-item label="请输入课时" prop="time" style="width:50%;">
                <el-input v-model="ruleForm.time"></el-input>
            </el-form-item>
            <el-form-item label="打折" prop="sale" style="width:50%;">
                <el-input v-model="ruleForm.sale"></el-input>
            </el-form-item>
            <el-form-item label="录制时间" required>
                <el-col :span="11">
                <el-form-item prop="date1">
                    <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 50%;"></el-date-picker>
                </el-form-item>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                <el-form-item prop="date2">
                    <el-time-picker placeholder="选择时间" v-model="ruleForm.date2" style="width: 50%;"></el-time-picker>
                </el-form-item>
                </el-col>
            </el-form-item>
            <el-form-item label="是否热推" prop="is_hot">
                <el-switch v-model="ruleForm.is_hot"></el-switch>
            </el-form-item>
            <el-form-item label="课程描述" prop="desc" style="width:50%;">
                <el-input type="textarea" v-model="ruleForm.desc"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
            </el-form>
    </div>
</template>

<script>
  import {mapState} from "vuex"
  export default {
    data() {
      return {
        ruleForm: {
          cover_img:"", //封面
          sale:"1", //
          teacher:'',
          name: '',
          type: '',
          date1: '',
          date2: '',
          is_hot: false,
          desc: '',
          price:"",
          time:""
        },
        rules: {
           sale:[
            { required: true, message: '折扣', trigger: 'blur' },
          
          ],
          time:[
            { required: true, message: '请输入课时', trigger: 'blur' },
            
          ],
          price:[
            { required: true, message: '请输入价格', trigger: 'blur' },
            {min:3,max:5,message:"不能低于100，不能高于10000",  trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入课程', trigger: 'blur' },
            
          ],
          teacher: [
            { required: true, message: '请输入课程老师', trigger: 'blur' },
       
          ],
          type: [
            { required: true, message: '选择课程', trigger: 'change' }
          ],
          date1: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          date2: [
            { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
          ],
         
          is_hot: [
            { required: true, message: '是否热推', trigger: 'change' }
          ],
          desc: [
            { required: true, message: '请填写课程描述', trigger: 'blur' }
          ]
        }
      };
    },
    computed:{
        ...mapState(["classType"])
    },
    methods: {
      handleAvatarSuccess(res){
         console.log(res.url)
         this.ruleForm.cover_img = res.url
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.ruleForm.cover_img===""){
              alert("封面必须上传")
            }else{
            this.$store.dispatch("addClass",{...this.ruleForm,
                  author:this.$store.state.user.nickname,
                  views:0,
                  isZan:false
                }).then(res=>{
                    this.$refs[formName].resetFields();
                    this.ruleForm.cover_img=""
                    alert("上传成功")
                })
            }
            // console.log(this.ruleForm,"ruleForm")
            // alert('submit!');


          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
      
      }
    }
  }
</script>

<style lang="scss">
.avatar-uploader [class^=el-icon-]{
    line-height: 50px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

 

</style>
<style >
  .mk .el-col-11{
      width: 18%;
  }
  .mk.el-col-2{
    width: -7%;
  }
  .avatar-uploader .el-upload{
    margin-left: 30px;
  }
</style>